Fast and easy syntax highlighting for selected languages.
Features
input languages
- C, C++
- Clojure (new)
- CSS
- Delphi
- diff
- ERB
- Go
- Groovy
- HAML (new)
- HTML
- Java
- JavaScript
- JSON
- Lua
- PHP
- Python
- Ruby
- Sass
- SQL
- Taskpaper
- XML
- YAML
output formats
- HTML snippet (inline styles)
- HTML page (CSS classes)
- Terminal (16 colors)
- JSON
- YAML
- Ruby Array (Tokens)
tools
-
coderay
executable - LoC counter (beta)
license
basic features
- fast: 1 MB/s!
- smart: doesn't choke on crazy code
- safe: handles bogus input gracefully
- pure Ruby implementation
- no dependencies
- plugin system
- line numbers
- nested languages
- encoding-aware (Ruby 1.9+)
- extensive test suite
- MRI 1.8.7
- MRI 1.9.3
- MRI 2.0+
- JRuby 1.7
- Rubinius 2.0
The latest stable version is 1.1.2. More features are planned for 1.2.
Usage & Live Demo
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# output as HTML div (using inline CSS styles) CodeRay.scan('puts "Hello, world!"', :ruby).div # ...with line numbers CodeRay.scan("5.times do\n puts 'Hello, world!'\nend", :ruby).div(:line_numbers => :table) # output as standalone HTML page (using CSS classes) CodeRay.scan('puts "Hello, world!"', :ruby).page # keep scanned tokens for later use tokens = CodeRay.scan('{ "just": "an", "example": 42 }', :json) # produce a token statistic tokens.statistic # count LoC (lines of code) CodeRay.scan("# comment\nputs 'Hello, world!'", :ruby).loc # => 1 # produce a HTML div, but with CSS classes tokens.div(:css => :class) # highlight a file (to HTML div); guess the file type base on the extension CodeRay.highlight_file(__FILE__) # re-using scanner and encoder with Duo CodeRay::Duo[:ruby, :div].encode('puts "Hello, world!"') |
More documentation is also available.